package jeconbond.economic.agent;

import jeconbond.automata.INode;
import jeconbond.automata.IOutcome;
import jeconbond.automata.behaviour.IAutomataWalkListener;
import jeconbond.automata.behaviour.IWalkStrategy;

public class AutomataAgent extends BaseEconomicProducer {
	private INode root;
	private INode state;
	private IWalkStrategy walkStrategy;
	private IAutomataWalkListener walkListener = IAutomataWalkListener.NOPWalkListener.INSTANCE;

	public AutomataAgent(INode root) {
		this.root = root;
		this.state = root;
	}

	public INode getRoot() {
		return root;
	}

	public INode getState() {
		return state;
	}

	public void setState(INode state) {
		this.state = state;
	}

	@Override
	public void processPreProduceStage() {
		merchant.processPurchase();
	}

	@Override
	public void processProduceStage() {
		IOutcome out = walkStrategy.doWalk(state);
		if (out == null) {
			walkListener.sameState(state);
		} else {
			walkListener.onWalkingPerformed(state, out.getDestination());
			state = out.getDestination();
		}
	}
	
	@Override
	public void processPostProduceStage() {
		merchant.processSelling();
	}
	
	public void setWalkStrategy(
			IWalkStrategy walkStrategy
	) {
		this.walkStrategy = walkStrategy;
	}
	
	public void setWalkingListener(IAutomataWalkListener walkListener) {
		this.walkListener = walkListener;
	}
}
